#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Inserts CSS links (added through server.add_css) in <head>.
When parameter type == 'link'
	This will insert
		<link rel="stylesheet" href="..." title="" />
When parameter type == 'import'
	This will insert
		@import url("...");
"""

import xml.dom.minidom
import time

def execute_minidom(server, document, parameters):
	# Parameter type
	if not 'type' in parameters or parameters['type'] not in ('import', 'link'):
		parameters['type'] = 'import'

	css = server.get_css()
	if len(css):
		for head in document.getElementsByTagName('head'):
			# Using @import url("...");
			if parameters['type'] == 'import':
				style = document.createElement('style')
				style.setAttribute('type', 'text/css')
				for c in css:
					style.appendChild(document.createTextNode('@import url("%s");\n' % c))
				head.appendChild(style)

			# Using <link rel="stylesheet" href="..." title="" />
			elif parameters['type'] == 'link':
				for c in css:
					link = document.createElement('link')
					link.setAttribute('rel', 'stylesheet')
					link.setAttribute('href', server.page_to_url(c))
					link.setAttribute('title', '')
					head.appendChild(link)
